//
//  LKSportControlViewController.m
//  SportDemo
//
//  Created by Lukj on 2017/4/21.
//  Copyright © 2017年 lukj. All rights reserved.
//

#import "LKSportControlViewController.h"
#import "LKSportMapViewController.h"
#import "LKSportTrackViewController.h"
@interface LKSportControlViewController ()
//  轨迹控制器
@property (nonatomic, strong) LKSportTrackViewController *trackVc;
//  轨迹按钮
@property (weak, nonatomic) IBOutlet UIButton *trackBtn;
//  总距离Label
@property (weak, nonatomic) IBOutlet UILabel *distanceLabel;
//  总时长Label
@property (weak, nonatomic) IBOutlet UILabel *timeLabel;
//  平均速度Label
@property (weak, nonatomic) IBOutlet UILabel *avgSpeedLabel;
//  继续按钮X方向的约束
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *continueBtnX;
//  结束按钮X方向的约束
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *finishBtnX;
@property (weak, nonatomic) IBOutlet UIButton *pauseBtn;
@property (weak, nonatomic) IBOutlet UIButton *continueBtn;
//  记录是否正在执行运动状态的操作
@property (nonatomic, assign) BOOL isAnimating;

@end

@implementation LKSportControlViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    //  一开始运动,就应该创建trackVc和mapVc,并且保证这里两个控制器不被释放
    self.trackVc = [[LKSportTrackViewController alloc] init];
    //  传递轨迹模型
    self.trackVc.track = self.track;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(locationUpdateNote:) name:@"LKSportLoactionDidUpdateNote" object:nil];
    
}

- (void)locationUpdateNote:(NSNotification *)note {
    //  设置数据
    self.distanceLabel.text = [NSString stringWithFormat:@"%.2f", self.track.totalDistace];
    
    self.timeLabel.text = self.track.totalTimeStr;
    
    self.avgSpeedLabel.text = [NSString stringWithFormat:@"%.2f", self.track.avgSpeed];
    
    if (!self.isAnimating) {//  当用户没有进行主动操作时, 执行自动处理
        //  获取当前的位置对象
        CLLocation *location = note.userInfo[@"LKSportLocationDidUpdateNoteAnimBtnKey"];
        
        if (location.speed == 0 && self.track.sportState == SportStateContinue) {//  判断速度, 并且运动状态为继续的时候才能暂停
            
            [self clickSportStateBtn:self.pauseBtn];
            
        }else if(location.speed > 0 && self.track.sportState == SportStatePause) {//    速度大于0, 并且运动状态为暂停的时候才能继续
            
            [self clickSportStateBtn:self.continueBtn];
        }
    }
    
}


- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    //  在界面传递子视图尺寸时, 注意在子视图布局完成后进行, 避免子视图没有布局完成时, 没有根据真实的设备尺寸来处理
    //  传递轨迹中心点
    self.trackVc.trackBtnCenter = self.trackBtn.center;
}


//  点击运动状态按钮
- (IBAction)clickSportStateBtn:(UIButton *)sender {
    //  用户开始操作后, 停止自动处理
    self.isAnimating = YES;
    
    self.track.sportState = sender.tag;
    
    switch (sender.tag) {
        case SportStatePause:{
            //  继续按钮左偏
            self.continueBtnX.constant -= 90;
            //  完成按钮右偏
            self.finishBtnX.constant += 90;
            //  隐藏暂停按钮
            self.pauseBtn.hidden = YES;
            [UIView animateWithDuration:0.25 animations:^{
                [self.view layoutIfNeeded];
            } completion:^(BOOL finished) {
                //  用户操作的动画执行完, 恢复自动处理
                self.isAnimating = NO;
            }];
        }
            break;
        case SportStateContinue:{
            //  继续按钮右偏
            self.continueBtnX.constant += 90;
            //  完成按钮左偏
            self.finishBtnX.constant -= 90;
            [UIView animateWithDuration:0.25 animations:^{
                [self.view layoutIfNeeded];
            } completion:^(BOOL finished) {
                //  动画完成, 显示暂停按钮
                self.pauseBtn.hidden = NO;
                //  用户操作的动画执行完, 恢复自动处理
                self.isAnimating = NO;
            }];
        }
            break;
        default:
            break;
    }
    
}
- (IBAction)clickTrackBtn:(id)sender {
    
    
    [self presentViewController:self.trackVc animated:YES completion:nil];
}

@end
